package com.summer.hundred;

public class ques8 {
    public static void main(String[] args) {
//        String s = " aa ";
//        System.out.println(s.length());
//        s= s.trim();
//        System.out.println(s+"---"+s.length());
//        if ('0'< '1') System.out.println("yes");
//        int num = '9'; //-45 +43 '0'48
//        System.out.println(num);
        String s = "+-12";
        System.out.println(myAtoi(s));
    }
    public static int myAtoi(String s) {
        long MAX =2147483647;
        long MIN = -2147483648;
        s = s.trim();
        String ss = "";
      // System.out.println(s.charAt(0)+"---"+(int)'4');
        if (s.length()==0){
            return 0;
        }else if (s.length()==1){
            if (s.charAt(0)<'0'&&s.charAt(0)>'9') {
                ss = s.charAt(0)+"";
                return (int)Long.parseLong(ss);
            }else {
                return 0;
            }
        }else {
            if (s.charAt(0)!='-'&&s.charAt(0)!='+') {
                if(s.charAt(0)<'0'||s.charAt(0)>'9'){
                    return 0;
                }
            }
            else if (s.charAt(1)>'0'&&s.charAt(1)<'9'){
                Character fuHao =' ';
                for(int i = 0;i < s.length();i++){
                    if(s.charAt(i)=='-'||s.charAt(i)=='+') {
                        fuHao = s.charAt(i);
                        break;
                    }
                }

                if (fuHao == null) fuHao='+';
                for(int i = 0;i < s.length();i++){
                    if(s.charAt(i)>='0'&&s.charAt(i)<='9') {
                        ss+=s.charAt(i)+"";

                        if(((i+1)>=s.length()||s.charAt(i+1)<'0'||s.charAt(i+1)>'9')){
                            break;
                        }
                    }
                }
                System.out.println(ss);
                long sNum =Long.parseLong(ss);
                if (fuHao=='-') sNum =-sNum;

                if (sNum<=MAX&&sNum>=MIN) {
                    return (int)sNum;
                }else if (sNum<MIN){
                    return (int)MIN;
                }else {
                    return (int)MAX;
                }
        }

        }
return 0;
    }

}
